<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="${ bp }/static/layui/css/layui.css">

    <style>

        .layui-form-item {
            position: relative;
        }

        .login-form .layui-form-item label {
            position: absolute;
            left: 1px;
            top: 1px;
            width: 38px;
            line-height: 36px;
            text-align: center;
            color: #d2d2d2;
        }

        .login-form .layui-form-item input {
            padding-left: 36px;
        }

    </style>
</head>
<body>
<div class="layui-container">
    <form class="layui-form layui-col-md4 layui-col-md-offset3">
        <div class="layui-form-item">
            <label class="layui-form-label">原密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="required" lay-reqText="原密码不能为空" autocomplete="off"
                       placeholder="请输入原密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input type="password" name="newPassword" lay-verify="required" lay-reqText="新密码不能为空" autocomplete="off"
                       placeholder="请输入新密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认新密码</label>
            <div class="layui-input-block">
                <input type="password" lay-verify="required|checkPassword" lay-reqText="确认新密码不能为空" autocomplete="off"
                       placeholder="请再次输入新密码" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-warm" lay-submit lay-filter="modify">确认修改</button>
            </div>
        </div>
    </form>

</div>

<script src="${ bp }/static/layui/layui.all.js"></script>
<script src="${ bp }/static/js/jquery-3.5.1.js"></script>
<script src="${ bp }/static/js/jquery.particleground.min.js"></script>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        let form = layui.form;
        let layer = layui.layer;
        let $ = layui.jquery;

        // 自定义校验规则
        form.verify({
            // value：表单的值、item：表单的DOM对象
            checkPassword: function (value, item) {
                if (value === $('[name="newPassword"]').val()) {

                } else {
                    return '两次输入的新密码不一致';
                }
            }
        });

        // 监听表单提交
        form.on('submit(modify)', function (data) {
            // 调用ajax提交修改数据
            $.ajax({
                url: '${ bp }/admin?method=modify',
                type: 'post',
                data: data.field,
                dataType: 'json',
                success: (result) => {
                    if (result.code === 0) {
                        layer.msg('修改成功！请重新登录！', {
                            time: 1000,
                            icon: 1
                        }, function () {
                            // 请求退出登录接口
                            top.location = '${ bp }/admin?method=logout';
                        });
                    } else {
                        layer.msg(result.msg, {
                            time: 2000,
                            icon: 2
                        });
                    }
                },
                error: () => {
                    console.log('请求失败！');
                }
            });
        });
    });
</script>
</body>
</html>
